package cn.brilliantfuture.commentry.servlet;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import cn.brilliantfuture.commentry.safe.VerifyMessageLocal;
import cn.brilliantfuture.commentry.user.UserMessageLocal;

public class MemberPasswdEditServlet extends AbstractHttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String appKey = request.getParameter("app_key");
		String appCode = request.getParameter("app_code");
		String version = request.getParameter("v");
		String timestamp = request.getParameter("timestamp");
		String format = request.getParameter("format");
		String userInfo = request.getParameter("user_info");
		
		String result = work(appKey, appCode, version, timestamp, format, userInfo);
		
		response.setContentType("application/json; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.getWriter().print(result);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	private String work(String appKey, String appCode, String version, String timestamp, String format, String userInfo) {
		JSONObject result = new JSONObject();
		
		if (!validParam(appKey, appCode, version, timestamp, format)) {
			result.accumulate("invoke_result", PARAM_ERROR);
			logger.info("Return result -> " + result.toString());
			return result.toString();
		}
		
		VerifyMessageLocal verifyMessageLocal = null;
		
		try {
			verifyMessageLocal = (VerifyMessageLocal) context.lookup("commentry-ear/VerifyMessageImpl/local");
		} catch (NamingException e1) {
			result.accumulate("invoke_result", INVOKE_FAILURE);
			logger.info("Return result -> " + result.toString());
			return result.toString();
		}
		
		Integer groupId = verifyMessageLocal.validAppParam(appKey, appCode);
		
		if (null == groupId){
			result.accumulate("invoke_result", PARAM_ERROR);
			logger.info("Return result -> " + result.toString());
			return result.toString();
		}
		
		userInfo = userInfo.replace("\r\n", "");
		
		JSONObject obj = JSONObject.fromObject(userInfo);
		
		String memberCode = obj.getString("user_id");
		String phone = obj.getString("phone");
		//String email = obj.getString("email");
		String oldPasswd = obj.getString("old_passwd");
		String newPasswd = obj.getString("new_passwd");
		
		UserMessageLocal userMessageLocal = null;
		
		try {
			userMessageLocal = (UserMessageLocal) context.lookup("commentry-ear/UserMessageImpl/local");
		} catch (NamingException e) {
			result.accumulate("invoke_result", INVOKE_FAILURE);
			logger.info("Return result -> " + result.toString());
			return result.toString();
		}
		
		if (userMessageLocal.updateMemberPasswd(memberCode, phone, oldPasswd, newPasswd)){
			result.accumulate("invoke_result", PAYMENT_SUCCESS);
			logger.info("Return result -> " + result.toString());
			return result.toString();
		} else {
			result.accumulate("invoke_result", PAYMENT_FAILURE);
			logger.info("Return result -> " + result.toString());
			return result.toString();
		}
	}
}
